//
//  BaseViewController.swift
//  SwiftDemo
//
//  Created by 宏泰财富 on 2018/11/8.
//  Copyright © 2018 宏泰财富. All rights reserved.
//

import UIKit

class BaseViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.white
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        let navigationBarHidden = self.navigationBarHidden
        self.navigationBarHidden = navigationBarHidden
        let navigationBarTintColor = self.navigationBarTintColor
        self.navigationBarTintColor = navigationBarTintColor
        let navigationBarTranslucent = self.navigationBarTranslucent
        self.navigationBarTranslucent = navigationBarTranslucent
        let navigationBarBackgroungImage = self.navigationBarBackgroungImage
        self.navigationBarBackgroungImage = navigationBarBackgroungImage
        let navigationBarShadowImage = self.navigationBarShadowImage
        self.navigationBarShadowImage = navigationBarShadowImage
        let navigationBarToolbarHidden = self.navigationBarToolbarHidden
        self.navigationBarToolbarHidden = navigationBarToolbarHidden
        let navigationBarTitleTextColor = self.navigationBarTitleTextColor
        self.navigationBarTitleTextColor = navigationBarTitleTextColor
        let navigationBarTitleTextFont = self.navigationBarTitleTextFont
        self.navigationBarTitleTextFont = navigationBarTitleTextFont
    }
 
    // 控制pop侧滑手势
    var interactivePopDisabled: Bool?
    
    //控制导航栏是否显示
    var navigationBarHidden: Bool? {
        didSet {
            self.navigationController?.setNavigationBarHidden(navigationBarHidden!, animated: true)
        }
    }
    
    //导航栏背景颜色
    var navigationBarTintColor: UIColor? {
        didSet {
            self.navigationController?.navigationBar.barTintColor = navigationBarTintColor!
        }
    }
    
    //控制导航栏是否具有透明效果
    var navigationBarTranslucent: Bool?{
        didSet {
            self.navigationController?.navigationBar.isTranslucent = navigationBarTranslucent ?? false
        }
    }
    
   //控制导航栏的背景图片,默认z为系统样式
    var navigationBarBackgroungImage: UIImage? {
        didSet {
            let stretchableBackgroundImg = __CGSizeEqualToSize(navigationBarBackgroungImage?.size ?? CGSize.zero, CGSize.zero) ? navigationBarBackgroungImage : navigationBarBackgroungImage?.stretchableImage(withLeftCapWidth: Int(navigationBarBackgroungImage?.size.width ?? 0.0 * 0.5), topCapHeight: Int(navigationBarBackgroungImage?.size.height ?? 0.0 * 0.5))
            self.navigationController?.navigationBar.setBackgroundImage(stretchableBackgroundImg, for: .default)
        }
    }
   
    //控制导航栏的线的图片
    var navigationBarShadowImage: UIImage? {
        didSet {
            self.navigationController?.navigationBar.shadowImage = navigationBarShadowImage
        }
    }
    
    //控制器是否显示底部工具条
    var navigationBarToolbarHidden: Bool?{
        didSet {
            self.navigationController?.toolbar.isHidden = navigationBarToolbarHidden ?? true
        }
    }
    
    //控制导航看标题颜色
    var navigationBarTitleTextColor: UIColor? {
        didSet {
         setNavigationBarTitleTextColorFont()
        }
    }
    
    //控制导航栏标题字体
    var navigationBarTitleTextFont: UIFont? {
        didSet {
            setNavigationBarTitleTextColorFont()
        }
    }
    
    //控制状态栏状态
    var statusBarStyle: UIStatusBarStyle? {
        didSet{
            setNeedsStatusBarAppearanceUpdate()
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        initData()
    }
    
    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
        initData()
    }
    
    private func initData(){
        self.interactivePopDisabled = false
        self.navigationBarHidden = false
        self.navigationBarTintColor = UIColor.white
        self.navigationBarTranslucent = true
        self.navigationBarBackgroungImage = nil
        self.navigationBarShadowImage = nil
        self.navigationBarToolbarHidden = true
        self.navigationBarTitleTextColor = UIColor.black
        self.navigationBarTitleTextFont = UIFont.boldSystemFont(ofSize: 18.0)
    }
    
    /*
     点击导航栏左侧返回按钮会调用此方法
     子类可重写
     */
     public func back() {
        self.navigationController?.popViewController(animated: true)
    }
    
    
    private func setNavigationBarTitleTextColorFont() {
        let textArrts = NSMutableDictionary.init(capacity: 2)
        textArrts.setValue(self.navigationBarTitleTextColor, forKey: (NSAttributedString.Key.foregroundColor as NSCopying) as! String)
        textArrts.setObject(self.navigationBarTitleTextFont as Any, forKey: NSAttributedString.Key.font as NSCopying)
        self.navigationController?.navigationBar.titleTextAttributes = textArrts as? [NSAttributedString.Key : Any]
    }
 
}


